﻿using System.Windows.Forms;
using GeoSketch2.Core;

namespace GeoSketch2.Archaeo
{
    /// <summary> 
    /// The tool to draw polylines
    /// </summary>
    public class TPolyline:BaseArchaeoTool
    {
        #region Fields
        /// <summary> 
        /// The current polyline
        /// </summary>
        public Polyline CurrentPolyline;
        /// <summary> 
        /// The current type of vertex
        /// </summary>
        public VertexType CurrentVertexType;

        //True if an arc is being drawn so, it's impossible to switch to line
        bool _ClicsRemaining;

        TBPolyline TBPolyline;
        #endregion

        #region Constructor
        /// <summary> 
        /// The tool to draw Archaeo polylines
        /// </summary>
        /// <param name="Viewport">The viewport to draw to</param>
        public TPolyline(Viewport Viewport)
            : base(Viewport,"Polyline")
        {
            CurrentVertexType = VertexType.Line;

            TBPolyline = new TBPolyline();
            TBPolyline.CurrentTool = this;
            TBPolyline.Parent = Viewport.DisplayControl;
            TBPolyline.Left = 300;
            TBPolyline.Show();
            TBPolyline.BringToFront();
        } 
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            ExternalPointSent(Viewport.Object2ImageTransformation.InverseTransform(MouseArgs.Location));
        }

        /// <summary>
        /// The method called when a text is sent to the tool
        /// </summary>
        public override void TextEntry(string Text)
        {
            ExternalPointSent(GetCoordinatesFromString(Text));
        }

        /// <summary>
        /// The method called when a point in object coordinates is sent to the tool
        /// </summary>
        public override void ExternalPointSent(Point2D64D Point)
        {
            if (CurrentPolyline == null)
            {
                NewPolyline();
            }
            if (CurrentPolyline.Vertices.Count > 0)
            {
                if (CurrentVertexType == VertexType.Line)
                {
                    if (!_ClicsRemaining)
                    {
                        CurrentPolyline.Vertices[CurrentPolyline.Vertices.Count - 1].Type = VertexType.Line;
                        CurrentPolyline.Vertices.Add(new Vertex(Point, CurrentVertexType));
                    }
                }
                else if (CurrentVertexType == VertexType.Arc)
                {
                    if (CurrentPolyline.Vertices[CurrentPolyline.Vertices.Count - 1].Type == VertexType.Line)
                    {
                        CurrentPolyline.Vertices[CurrentPolyline.Vertices.Count - 1].Type = VertexType.Arc;
                    }
                    _ClicsRemaining = !_ClicsRemaining;
                    CurrentPolyline.Vertices.Add(new Vertex(Point, CurrentVertexType));
                }
            }
            else
            {
                CurrentPolyline.Vertices.Add(new Vertex(Point, CurrentVertexType));
            }
            RefreshViewport();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Creates a new polyline
        /// </summary>
        public void NewPolyline()
        {
            CurrentPolyline = new Polyline(null,Viewport.CurrentLayer, Viewport.CurrentExcavationArea, Viewport.CurrentLineType,Viewport.CurrentColor);
            _ClicsRemaining = false;
            Viewport.GeoObjects.Add(CurrentPolyline);
        }

        /// <summary>
        /// Closes the current polyline
        /// </summary>
        public void ClosePolyline()
        {
            if (CurrentPolyline != null)
            {
                CurrentPolyline.Vertices[CurrentPolyline.Vertices.Count - 1].Type = VertexType.Line;
                CurrentPolyline.Vertices.Add(CurrentPolyline.Vertices[0]);
                CurrentPolyline = null;
            }
        }

        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Clic or enter coordinates");
        }

        /// <summary>
        /// This method is called when deselcting the tool, or swithcing to another one
        /// </summary>
        public override void Release()
        {
            this.TBPolyline.Dispose();
        }
        #endregion
    }
}
